﻿using System;

using zijian666.Converts.Abstractions;
using zijian666.Converts.Convertor.Base;
using zijian666.Converts.Core;
using zijian666.Converts.Extensions;

namespace zijian666.Converts.Convertor;

/// <summary>
/// <seealso cref="DateTime" /> 转换器
/// </summary>
public class DateTimeOffsetConvertor : FromConvertor<DateTimeOffset>, IFrom<object, DateTimeOffset>, IFrom<DateTime, DateTimeOffset>
{
    /// <inheritdoc/>
    public ConvertResult<DateTimeOffset> From(IConvertContext context, DateTime input) => new(new DateTimeOffset(input));

    /// <inheritdoc/>
    public ConvertResult<DateTimeOffset> From(IConvertContext context, object input)
    {
        var r = context.Convert<DateTime>(input);
        if (r.Success)
        {
            return new(new DateTimeOffset(r.Value));
        }
        return context.ConvertFail(this, input);
    }
}
